﻿Public Class ManejoDeCarga
    Dim bd As AccesoADatos

    Public Sub New()
        bd = New AccesoADatos()
    End Sub

    Public Sub cargarCombo(ByRef combo As ComboBox, ByVal tabla As Data.DataTable, ByVal pk As String, ByVal descripcion As String)
        combo.DataSource = tabla
        combo.ValueMember = pk
        combo.DisplayMember = descripcion
    End Sub

    Public Sub cargarCombo(ByRef combo As ComboBox, ByVal nombreTabla As String, ByVal pk As String, ByVal descripcion As String)
        combo.DataSource = bd.leerTabla(nombreTabla)
        combo.ValueMember = pk
        combo.DisplayMember = descripcion
    End Sub

    Public Sub cargarLista(ByRef lista As ListBox, ByVal tabla As Data.DataTable, ByVal pk As String, ByVal descripcion As String)
        lista.DataSource = tabla
        lista.ValueMember = pk
        lista.DisplayMember = descripcion
    End Sub

    Public Sub cargarLista(ByRef lista As ListBox, ByVal columnas As String, ByVal nombreTabla As String, ByVal condicion As String, ByVal pk As String, ByVal descripcion As String)
        lista.DataSource = bd.leerTabla(columnas, nombreTabla, condicion)
        lista.ValueMember = pk
        lista.DisplayMember = descripcion
    End Sub

    Public Sub cargarLista(ByRef lista As ListBox, ByVal columnas As String, ByVal nombreTabla As String, ByVal pk As String, ByVal descripcion As String)
        lista.DataSource = bd.leerTabla(columnas, nombreTabla)
        lista.ValueMember = pk
        lista.DisplayMember = descripcion
    End Sub

    Public Sub cargarGrilla(ByRef grilla As DataGridView, ByVal nombreTabla As String)
        Dim tabla As Data.DataTable = bd.leerTabla(nombreTabla)
        grilla.DataSource = tabla
        formatoGrilla(grilla, tabla)
    End Sub

    Public Sub cargarGrilla(ByRef grilla As DataGridView, ByVal columnas As String, ByVal nombreTabla As String)
        Dim tabla As Data.DataTable = bd.leerTabla(columnas, nombreTabla)
        grilla.DataSource = tabla
        formatoGrilla(grilla, tabla)
    End Sub

    Public Sub cargarGrilla(ByRef grilla As DataGridView, ByVal columnas As String, ByVal nombreTabla As String, ByVal condicion As String)
        Dim tabla As Data.DataTable = bd.leerTabla(columnas, nombreTabla, condicion)
        grilla.DataSource = tabla
        formatoGrilla(grilla, tabla)
    End Sub

    Private Sub formatoGrilla(ByRef grilla As DataGridView, ByVal tabla As Data.DataTable)
        Dim contador As Integer = 0
        While tabla.Columns.Count() > contador
            grilla.Columns(contador).HeaderText = tabla.Columns(contador).Caption
            contador += 1
        End While
        grilla.Sort(grilla.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
    End Sub
End Class
